package com.sanrenzu.chunbo.ui.fenlei;

import android.os.Handler;
import android.os.Looper;
import com.sanrenzu.chunbo.bean.zxb.LaoTangBean;
import com.sanrenzu.chunbo.bean.zxb.LaoTangChildBean;
import com.sanrenzu.chunbo.bean.zxb.LunBoBean;
import com.sanrenzu.chunbo.bean.zxb.GuanggaoLunboBean;
import com.sanrenzu.chunbo.bean.zxb.QiangGouBean;
import com.sanrenzu.chunbo.bean.zxb.QiangGouChildBean;
import com.sanrenzu.chunbo.bean.zxb.ReMaiBean;
import com.sanrenzu.chunbo.bean.zxb.ReMaiChildBean;
import com.sanrenzu.chunbo.bean.zxb.TuiJianBean;
import com.sanrenzu.chunbo.bean.zxb.TuiJianChildBean;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by My on 2016/8/30.
 */
public class FenLeiPresenter implements FenLeiContract.Presenter{

    private FenLeiContract.View view;
    private FenLeiContract.Modle modle;

    public FenLeiPresenter(FenLeiContract.View view){
        this.view=view;
        modle = new FenLeiModle();
    }

    @Override
    //表明我要做的事（网络数据访问），然后通过modle获取网络数据， 通过view确定我是否展现成功，展现了json数据
    //轮播
    public void getVertical() {
        modle.getVertical(new Callback<LunBoBean>() {
            @Override
            public void onResponse(Call<LunBoBean> call, Response<LunBoBean> response) {
                LunBoBean bean = response.body();
                final List<GuanggaoLunboBean> lunbos = bean.getGuanggaoLunbo();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalDataBean(lunbos);
                    }
                });
            }

            @Override
            public void onFailure(Call<LunBoBean> call, Throwable t) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalFailed("网络数据获取失败");
                    }
                });
            }
        });
    }
    //表明我要做的事（网络数据访问），然后通过modle获取网络数据， 通过view确定我是否展现成功，展现了json数据
    //抢购
    @Override
    public void getVerticalQiangGouBean() {
        modle.getVerticalQiangGouBean(new Callback<QiangGouBean>() {
            @Override
            public void onResponse(Call<QiangGouBean> call, Response<QiangGouBean> response) {
                QiangGouBean bean = response.body();
                final List<QiangGouChildBean> bens = bean.getProductQgList();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalQiangGouChildBean(bens);

                    }
                });
            }

            @Override
            public void onFailure(Call<QiangGouBean> call, Throwable t) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalFailed("抢购网络获取数据失败");
                    }
                });
            }
        });
    }

    @Override
    public void getVerticalTuiJianBean() {
        modle.getVerticalTuiJianBean(new Callback<TuiJianBean>() {
            @Override
            public void onResponse(Call<TuiJianBean> call, Response<TuiJianBean> response) {
                TuiJianBean bean = response.body();
                final List<TuiJianChildBean> beans = bean.getProductTjList();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalTuiJianChildBean(beans);
                    }
                });
            }

            @Override
            public void onFailure(Call<TuiJianBean> call, Throwable t) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalFailed("推荐网络数据获取失败");
                    }
                });
            }
        });
    }

    @Override
    public void getVerticalReMaiBean() {
        modle.getVerticalReMaiBean(new Callback<ReMaiBean>() {
            @Override
            public void onResponse(Call<ReMaiBean> call, Response<ReMaiBean> response) {
                ReMaiBean bean = response.body();
                final List<ReMaiChildBean> beans = bean.getProductRmList();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalReMaiChildBean(beans);
                    }
                });
            }

            @Override
            public void onFailure(Call<ReMaiBean> call, Throwable t) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalFailed("热卖网络数据获取失败");
                    }
                });
            }
        });
    }

    @Override
    public void getVerticalLaoTangBean() {
        modle.getVerticalLaoTangBean(new Callback<LaoTangBean>() {
            @Override
            public void onResponse(Call<LaoTangBean> call, Response<LaoTangBean> response) {
                LaoTangBean bean = response.body();
                final List<LaoTangChildBean> beans = bean.getXxGuanggao();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalLaoTangChildBean(beans);
                    }
                });
            }

            @Override
            public void onFailure(Call<LaoTangBean> call, Throwable t) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetVerticalFailed("老汤网络获取失败");
                    }
                });
            }
        });
    }

}
